package com.molichuxing.services.business.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.molichuxing.services.business.convertor.Convert;
import com.molichuxing.services.business.dto.request.create.OrderPurchaseCreateBizDto;
import com.molichuxing.services.business.dto.request.modify.OrderPayModifyBizDto;
import com.molichuxing.services.business.dto.response.OrderMileageBizDto;
import com.molichuxing.services.business.dto.response.OrderPurchaseBizDto;
import com.molichuxing.services.business.service.OrderPurchaseBizService;
import com.molichuxing.services.infrastructure.dto.request.create.OrderCreateDto;
import com.molichuxing.services.infrastructure.dto.response.OrderMileageDto;
import com.molichuxing.services.infrastructure.service.OrderMileageService;
import com.molichuxing.services.infrastructure.service.OrderService;
import com.molichuxing.services.infrastructure.service.OrderSubscribeService;
import com.molichuxing.services.property.OrderMileagePayStatusEnum;
import com.molichuxing.services.property.OrderTypeEnum;
import com.molichuxing.services.utils.OrderUtil;

/**
 * 加购里程biz服务
 * 
 * @author zoumingyu
 * @date 2019年9月23日
 *
 */
@Service("orderPurchaseBizService")
public class OrderPurchaseBizServiceImpl implements OrderPurchaseBizService {
	private static final Logger logger = LoggerFactory.getLogger(OrderPurchaseBizServiceImpl.class);

	@Resource
	private OrderMileageService orderMileageService;

	@Resource
	private OrderService orderService;

	@Resource
	private OrderSubscribeService orderSubscribeService;

	/**
	 * 加购里程信息(不包括已取消)
	 * 
	 * @param masterOrderCode
	 *            主订单号
	 * @return
	 */
	@Override
	public OrderPurchaseBizDto getOrderPurchase(Long masterOrderCode) {
		OrderPurchaseBizDto result = null;
		// 根据订单号查询
		List<OrderMileageDto> orderMileageDtoList = orderMileageService.getEffectiveOrderMileageList(masterOrderCode);
		if (orderMileageDtoList == null || orderMileageDtoList.isEmpty()) {
			return result;
		}
		List<OrderMileageBizDto> mileages = new ArrayList<>();
		// 加购里程数(总),超里程数(总)
		Integer totalPurchaseMileage = 0, totalBeyondMileage = 0;
		// 加购里程金额(总),超里程金额(总)
		BigDecimal totalPurchaseFee = BigDecimal.ZERO, totalBeyondFee = BigDecimal.ZERO;
		for (OrderMileageDto orderMileageDto : orderMileageDtoList) {
			if (OrderMileagePayStatusEnum.PREPAID.equals(orderMileageDto.getStatus())) {
				// 已支付
				totalPurchaseMileage += orderMileageDto.getPurchaseMileage();
				totalBeyondMileage += orderMileageDto.getBeyondMileage();
				totalPurchaseFee = totalPurchaseFee.add(orderMileageDto.getPurchaseFee());
				totalBeyondFee = totalBeyondFee.add(orderMileageDto.getBeyondFee());
			}
			OrderMileageBizDto orderMileageBizDto = Convert.toOrderMileageBizDto(orderMileageDto);
			mileages.add(orderMileageBizDto);
		}
		result = new OrderPurchaseBizDto();
		result.setTotalPurchaseMileage(totalPurchaseMileage);
		result.setTotalPurchaseFee(totalPurchaseFee);
		result.setTotalBeyondMileage(totalBeyondMileage);
		result.setTotalBeyondFee(totalBeyondFee);
		result.setDetails(mileages);

		return result;
	}

	/**
	 * 新增加购订单
	 * 
	 * @param createDto
	 * @return 订单号
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
	public Long createOrderPurchase(OrderPurchaseCreateBizDto createDto) {
		// 订单号
		Long orderCode = OrderUtil.getOrderCode();
		// 新增订单表信息
		orderService.create(new OrderCreateDto(orderCode, OrderTypeEnum.RELET_MILEAGE));
		// 新增加购里程订单
		orderMileageService.create(Convert.toOrderMileageCreateDto(orderCode, createDto));

		return orderCode;
	}

	/**
	 * 支付成功后修改订单信息
	 * 
	 * @param modifyDto
	 * @return
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
	public Boolean modifyPaySuccess(OrderPayModifyBizDto modifyDto) {
		Boolean result = false;
		if (orderMileageService.modifyPay(Convert.toOrderPayModifyDto(modifyDto))) {
			// 查询
			OrderMileageDto orderMileageDto = orderMileageService.getOrderMileage(modifyDto.getOrderCode());
			// 总购买里程（加购里程+超里程数）
			Integer totalPayMileage = orderMileageDto.getPurchaseMileage() + orderMileageDto.getBeyondMileage();
			result = orderSubscribeService.modifyWarningMileageByPurchase(orderMileageDto.getMasterOrderCode(),
					totalPayMileage);
		}

		return result;
	}

}
